package com.xcx.robot.mirai.factory.facade;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.xcx.robot.mirai.factory.BaseFacade;
import com.xcx.robot.mirai.jxapi.Result;
import com.xcx.robot.mirai.key.KeyWord;
import com.xcx.robot.mirai.key.PositionLocation;
import com.xcx.robot.mirai.util.KeyUtils;
import com.xcx.robot.mirai.util.SenderHelper;
import net.mamoe.mirai.contact.Contact;
import net.mamoe.mirai.message.data.PlainText;
import org.springframework.stereotype.Service;

/**
 * @Author: 邢晨旭
 * @Date: 2021/5/26 16:18
 * @Description:
 */
@Service
public class MacroFacade implements BaseFacade {

    public KeyWord keyWord = KeyWord.MACRO;
    private final String url = "https://jx3api.com/app/macro?name=%s";

    @Override
    public void execute(Contact sender, Contact receiver, String content) {
        PositionLocation positionLocation = PositionLocation.get(KeyUtils.getKeyByWord(content, 2));
        if(positionLocation!=null){
            String s = HttpUtil.get(String.format(url, positionLocation.fullName));
            Result result = JSONObject.parseObject(s, Result.class);
            if(result.getCode()!=200){
                SenderHelper.sendMessage(receiver,new PlainText("暂时没有查到哦"));
                return;
            }
            SenderHelper.sendMessage(receiver,new PlainText("奇穴：").plus(result.getData().getHoles())
            .plus("\n").plus(result.getData().getCommand()));
        }
    }

    @Override
    public KeyWord keyWord() {
        return keyWord;
    }
}
